﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace QueryZJK.Converters
{
    public abstract class BooleanConverter<T> : IValueConverter
    {
        protected BooleanConverter(T trueValue, T falseValue)
        {
            this.TrueValue = trueValue;
            this.FalseValue = falseValue;
        }

        public T TrueValue { get; set; }

        public T FalseValue { get; set; }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Object.Equals(value, true) ? this.TrueValue : this.FalseValue;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Object.Equals(value, this.TrueValue) ? true : false;
        }
    }

    #region Visibility
    class BooleanToVisibilityConverter : BooleanConverter<Visibility>
    {
        #region ctor
        static BooleanToVisibilityConverter()
        {
        }
        private BooleanToVisibilityConverter()
            : base(Visibility.Visible, Visibility.Collapsed)
        {
        }
        #endregion

        private static BooleanToVisibilityConverter converter;
        public static BooleanToVisibilityConverter Instance
        {
            get { return converter ?? (converter = new BooleanToVisibilityConverter()); }
        }
    }

    class BooleanToVisibilityOppositeConverter : BooleanConverter<Visibility>
    {
        #region ctor
        static BooleanToVisibilityOppositeConverter()
        {
        }
        private BooleanToVisibilityOppositeConverter()
            : base(Visibility.Collapsed, Visibility.Visible)
        {
        }
        #endregion

        private static BooleanToVisibilityOppositeConverter converter;
        public static BooleanToVisibilityOppositeConverter Instance
        {
            get { return converter ?? (converter = new BooleanToVisibilityOppositeConverter()); }
        }
    }
    #endregion

    public class CountToVisibleConverter : IValueConverter
    {
        #region ctor
        static CountToVisibleConverter()
        {
        }
        private CountToVisibleConverter()
        {
        }
        #endregion

        private static CountToVisibleConverter converter;
        public static CountToVisibleConverter Instance
        {
            get { return converter ?? (converter = new CountToVisibleConverter()); }
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return int.Parse(value.ToString()) == 0 ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
